# ----------------------------------------------------------
#   |  author  : wenqvip                  |     \|/      |
#   |  email   : wenqvip@gmail.com        |    --+--     |
#   |  version : 0.1.2                    |     /|\      |
# ----------------------------------------------------------

OUT=tinyweb

CC=gcc
CXX=g++
LD=gcc

SRCS=$(wildcard *.cpp)
SRCS+=$(wildcard *.c)
REJECT_SRCS=

DEFINES=
INCLUDES=
ARG_m32=-m32
ARG_v=#-v
ARG_g=-g
ARG_O=#O2

SUBDIRS=cgi-bin

CFLAGS=$(ARG_g) $(ARG_O) $(ARG_v) $(ARG_m32) $(DEFINES)
CXXFLAGS=$(ARG_g) $(ARG_O) $(ARG_v) $(ARG_m32) $(DEFINES)
LDFLAGS=$(ARG_v) 

ALL_OBJS=$(addsuffix .o,$(basename $(SRCS)))
REJECT_OBJS=$(addsuffix .o,$(basename $(REJECT_SRCS)))
OBJS=$(filter-out $(REJECT_OBJS),$(ALL_OBJS))
DIR_OBJS=objs

DIR_DEPS=.deps
DEPS=$(patsubst %.o,$(DIR_DEPS)/%.d,$(OBJS))

vpath %.o ./$(DIR_OBJS)/

all : subsystem $(OUT)

$(OUT) : $(OBJS)
	$(LD) $(ARG_m32) $(patsubst %.o,./objs/%.o,$(OBJS)) $(LDFLAGS) -o $(OUT)

%.o : %.cpp
	[ -d $(DIR_OBJS) ] || mkdir -p $(DIR_OBJS)
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o ./objs/$@

%.o : %.c
	[ -d $(DIR_OBJS) ] || mkdir -p $(DIR_OBJS)
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o ./objs/$@

$(DIR_DEPS)/%.d : %.cpp
	[ -d $(DIR_DEPS) ] || mkdir -p $(DIR_DEPS)
	$(CXX) $(INCLUDES) -MM -MD $< -o $@

$(DIR_DEPS)/%.d : %.c
	[ -d $(DIR_DEPS) ] || mkdir -p $(DIR_DEPS)
	$(CC) $(INCLUDES) -MM -MD $< -o $@

-include $(DEPS)

subsystem:
	@for d in $(SUBDIRS); do \
	  if test -d "$$d"; then \
	    cd $$d && $(MAKE); \
		cd ..; \
	  fi; \
	done;

clean :
	rm -rf objs $(OUT)
	@echo all cleaned

.PHONY : all clean
